<?php
/**
 * xushuhui
 * 2017/8/14
 * 16:32
 */

namespace app\index\service;

use app\index\model\Order;
use think\Db;
use think\Exception;
use think\Log;
use think\Loader;
Loader::import('WxPay.WxPay', EXTEND_PATH, '.Api.php');
class WxNotify extends \WxPayNotify
{
	public function NotifyProcess($data, &$msg){
		if ($data['result_code'] == 'SUCCESS') {
			try {
				$order = Order::where('orderNo', $data['out_trade_no'])->find();
				if($order->status == 1){
					$this->updateOrderStatus($order->id, 2);
				}
				return true;
			} catch (Exception $ex) {

				Log::error($ex);
				// 如果出现异常，向微信返回false，请求重新发送通知
				return false;
			}
		}
		return true;
	}
	private function updateOrderStatus($orderID, $status)
	{
		return Order::where('id', '=', $orderID)
				->update(['status' => $status]);
	}
}